package root;

import javax.swing.plaf.basic.BasicLookAndFeel;

import reader.DicoAccessor;
import game.Grid;

public class Word {
	
	private Grid mGrille;
	private int mColumn;
	private int mRow;
	
	private Grid.Direction mDirection;
	
	private boolean mHasChanged;
	private boolean mIsExist;
	
	private String mCurrentString;
	
	public Word(Grid gridOfWords, int _row, int _column, Grid.Direction direction) {
		mColumn = _column;
		mRow = _row;
		mDirection = direction;
		
		mHasChanged = true;
		
		mGrille = gridOfWords;
		
		mCurrentString = mGrille.getWord(mRow,mColumn,mDirection);
	}
	
	public void onLettersRollBack(Grid g)
	{
		mHasChanged = false;
		mIsExist = true;
		mGrille = g;
	}
	
	public int getColumn() {
		return mColumn;
	}

	public int getRow() {
		return mRow;
	}

	public Grid.Direction getDirection() {
		return mDirection;
	}

	public void onLettersChanged(Grid g)
	{
		String newString =  g.getWord(mRow,mColumn,mDirection);
		if(!mCurrentString.equals(newString))
		{
			mHasChanged = true;
			mCurrentString = newString;
		}
		
		mGrille = g;
	}
	
	public boolean isExist()
	{
		if(mHasChanged)
		{
			mIsExist = DicoAccessor.DICO_HMM.hasAtLeastOnMatch(createPattern());
			mHasChanged = false;
		}
		return mIsExist;
	}
	
	
	public String createPattern()
	{
		String pattern = mCurrentString;
		for(int i=0; i < pattern.length(); i++)
		{
			if(!String.valueOf(pattern.charAt(i)).matches("[a-zA-Z]"))
			{
				pattern = pattern.replace(pattern.charAt(i),'.');
			}
		}
		
		return pattern;
	}
	
	public int getNbConstraint()
	{
		String word = mCurrentString;
		int nbConstraint = 0;
		for(int i=0; i < word.length(); i++)
		{
			if(word.charAt(i)!=Grid.BLANK)
				nbConstraint++;
		}
		return nbConstraint;
	}
	
	public String toString()
	{
		return String.valueOf(mGrille.getWord(mRow,mColumn,mDirection));
	}

	public boolean isFull() {
		return !mGrille.getWord(mRow,mColumn,mDirection).contains(String.valueOf(Grid.BLANK));
	}

}
